import girder_client

gc = girder_client.GirderClient(apiUrl='http://127.0.0.1:8080/api/v1')
gc.authenticate('admin', '123456')

items = gc.listItem("6678ff7d5b1db0c8ee86ec89")
json_path = "/media/hsmy/wanghao_18T/dataset/三级淋巴结构_0624/mask_his/"
for items in items:
    files = gc.listFile(items['_id'])
    for file in files:
        # print(f"{file['_id']}-{file['itemId']}")
        file_name = file['name'].split('.')[0]
        annotation_file_path = json_path + file_name + '.json'
        id_ = file['itemId']
        wsis = gc.listFile(id_)
        for wsi in wsis:
            wsi_id = wsi['_id']
            if str(wsi['exts']) == "['json']":
                print(wsi)
                gc.delete(f"/file/{wsi_id}")

            if str(wsi['exts']) == "['svs']":
                reference = ('{"identifier":"LargeImageAnnotationUpload","itemId":"' + id_
                             + '","fileId":"' + wsi_id
                             + '","userId":"6670179596177422aa6546a6"}')
                print(reference)
                gc.uploadFileToItem(id_, annotation_file_path, reference=reference)
